% This code produces statistics used in Figure 3

function [IS_path , OOS_path] = OOS_EIS_1(DATA , DATA_CellNames , RHS_Names , RHS_signs ,horizon , T_burn)

% INPUT:
% --DATA: cell that contains simulated data
% --DATA_CellNames: variable names matched to cells
% --RHS_Names: RHS variables used in comparison
% --RHS_sign: sign constraint on RHS variables
% --horizon: name of the column to display in the Table
% --T_burn: Initial periods used to calculate mean

%% 1. Get simulated data series
% Log excess returns
exret = DATA{strcmp(DATA_CellNames , 'exret' )};

T = size(exret , 1);
N = size(exret , 2);

% Moving sum of log excess returns for LHS
exret_MS = [nan(horizon - 1 , N);...
            movsum(exret , horizon , 'Endpoints' , 'discard')];%From 1 to T with NaN in the beginning
        
%% 2. Use simple historical mean  

% In-sample residuals
e_IS_avg = exret_MS(horizon + 1 : end , :) - mean(exret) * horizon;

% Out-of-sample residuals
exret_cummean = cumsum(exret)./repmat((1 : T)' , 1 , N);%Cumulative mean
e_OOS_avg = exret_MS(T_burn + horizon : end , :) ...
          - exret_cummean(T_burn : T - horizon , :) * horizon;
      
%% 3. Use proposed predictors           
K = length(RHS_Names);

IS_path = cell(K , 1);
OOS_path = cell(K , 2);
for j = 1 : K
    RHS_temp = DATA{strcmp(DATA_CellNames , RHS_Names{j})};
    sign_RHS_temp = RHS_signs(j);
    
    e_IS_temp = nan(T - horizon , N);
    e_OOS_uc_temp = nan(T - T_burn - horizon + 1 , N);%Unconstrained
    e_OOS_c_temp = nan(T - T_burn - horizon + 1 , N);%Constrained
    for i = 1 : N
        % In-sample regression    
        b_IS_temp = olsgmm(exret_MS(horizon + 1 : end , i) , [ones(T - horizon , 1) , RHS_temp(1 : end - horizon , i)] , 0 , -1);    
        e_IS_temp(: , i) = exret_MS(horizon + 1 : end , i) - [ones(T - horizon , 1) , RHS_temp(1 : end - horizon , i)] * b_IS_temp;
        
        % Out-of-sample regression
        for m = 1 : T - T_burn - horizon + 1
            % Inside the available sample
            b_OOS_uc_temp = olsgmm(exret_MS(horizon + 1 : T_burn + m - 1 , i) , ...
                                   [ones(T_burn + m - horizon - 1 , 1) , RHS_temp(1 : T_burn + m - horizon - 1 , i)] , 0 , -1);
                               
            res_OOS_uc_temp = exret_MS(T_burn + horizon + m - 1 , i)...
                                 - [1 , RHS_temp(T_burn + m - 1 , i)] * b_OOS_uc_temp;                   
            
            b_OOS_c_temp = olsgmm(exret_MS(horizon + 1 : T_burn + m - 1 , i) , ...
                                  ones(T_burn + m - horizon - 1 , 1) , 0 , -1);
            
            res_OOS_c_temp = exret_MS(T_burn + horizon + m - 1 , i)...
                           - b_OOS_c_temp;                   
                              
            e_OOS_uc_temp(m , i) = res_OOS_uc_temp;
            if sign(b_OOS_uc_temp(end)) ~= sign_RHS_temp
                e_OOS_c_temp(m , i) = res_OOS_c_temp;
            elseif sign(b_OOS_uc_temp(end)) == sign_RHS_temp
                e_OOS_c_temp(m , i) = res_OOS_uc_temp;
            end
        end
    end
    
    IS_path{j} = mean(cumsum(e_IS_avg.^2) - cumsum(e_IS_temp.^2) , 2);
    OOS_path{j , 1} = mean(cumsum(e_OOS_avg.^2) - cumsum(e_OOS_uc_temp.^2) , 2);
    OOS_path{j , 2} = mean(cumsum(e_OOS_avg.^2) - cumsum(e_OOS_c_temp.^2) , 2);
    
end


end







